(function ($) {
  $.fn.initInputChange = function(data,name,id) {
//        var fuzzyKey = ["z","c","s","an","en","in","ian","uan","iang"];//模糊音数组
//        var fuzzyValue = ["zh","ch","sh","ang","eng","ing","iang","uang","ian"];//模糊音数组
  $(this).focus(function () {
    //先拼接字符串
    var str = $(this).val();
    var obj = "#" + $(this).attr("id");
    if (str == "" || str == null || str == undefined) {
      //全查询
      getCandidate(obj, data, str);
    } else {
      //关键字查询
      getCandidate(obj, data, str);
    }
    var inputWidth = $(this).width() + 24;
    $(this).siblings(".input-candidate").width(inputWidth).show().find("li");
  });

  $(this).keyup(function () {
    getCandidate("#" + $(this).attr("id"), data, $(this).val());
    var inputWidth = $(this).width() + 24;
    $(this).siblings(".input-candidate").width(inputWidth).show().find("li");
  });

  $(this).blur(function () {
    $(this).siblings(".input-candidate").hide();
  });

  //获取所有候选，拼接字符串
  function getCandidate(obj, data, searchStr) {

    var arr = [];
    for (var i = 0; i < data.length; i++) {
      var dataStrPy = pinyin.getFullChars(data[i][name]);
      var searchStrPy = pinyin.getFullChars(searchStr);
      dataStrPy = dataStrPy.replace("-","");
      searchStrPy = searchStrPy.replace("-","");
      if (searchStrPy == "" || dataStrPy.indexOf(searchStrPy) != -1) {
        arr.push({name:data[i][name],id:data[i][id]});
        continue;
      }
      var fsearchStr = pinyin.getCamelChars(searchStr).toLocaleUpperCase().replace("-","");
      var fdataStrPy = pinyin.getCamelChars(data[i][name]).replace("-","");
      if(fdataStrPy.indexOf(fsearchStr) != -1){
        arr.push({name:data[i][name],id:data[i][id]});
      }
    }
    var str = "<ul class='input-candidate' id='input-candidate'>";
    for(var i=0;i<arr.length;i++){
      str += "<li><p id='"+arr[i].id+"'>"+arr[i].name+"</p></li>";
    }
    str += "</ul>";
    $(obj).siblings(".input-candidate").remove();
    $(obj).parent().append(str).find("li").unbind().on("mousedown", function () {
      $(obj).val($(this).find("p").html());
    });
  }
}
})(jQuery)